import { getAllCompany } from '@/api/base'
import moment from 'moment'
export const mixinDate = {
  computed: {
    isData() {
      return (val) => {
        // 时间的计算
        if (val) {
          const startDate = moment(this.ShowDate(0)).format('YYYY-MM-DD')
          let value = val.applyDate || val.createDate
          let applyDate = value.split(' ')[0]
          const endDate = moment(applyDate).format('YYYY-MM-DD')
          let time = moment(startDate).diff(endDate, 'day')
          if (time > 5) return false
          else return true
        }
      }
    },
  },
  created() {},
  mounted() {

  },
  data() {
    return {
      pickerOptionsItem: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      }
    }
  },
  methods: {
     // 日期距离
     ShowDate(date) {
      var num = date
      let n = num
      let d = new Date()
      let year = d.getFullYear()
      let mon = d.getMonth() + 1
      let day = d.getDate()
      if (day <= n) {
        if (mon > 1) {
          mon = mon - 1
        } else {
          year = year - 1
          mon = 12
        }
      }
      d.setDate(d.getDate() - n)
      year = d.getFullYear()
      mon = d.getMonth() + 1
      day = d.getDate()
      let s = year + '-' + (mon < 10 ? '0' + mon : mon) + '-' + (day < 10 ? '0' + day : day)
      return s
    },
     // 年
     ShowYear(date) {
      var num = date
      let n = num
      let d = new Date()
      let year = d.getFullYear()
      let mon = d.getMonth() + 1
      let day = d.getDate()
      if (day <= n) {
        if (mon > 1) {
          mon = mon - 1
        } else {
          year = year - 1
          mon = 12
        }
      }
      d.setDate(d.getDate() - n)
      year = d.getFullYear()
      mon = d.getMonth() + 1
       day = d.getDate()
      //需要字符串类型
      let s = year  +'' //'-' + (mon < 10 ? '0' + mon : mon) + '-' + (day < 10 ? '0' + day : day)
      return s
    },
    // 表格隔行变色方法
    tableRowClassName({ row, rowIndex }) {
      if ([1, 3, 5, 7, 9].includes(rowIndex)) {
        return 'success-row';
      }
      return '';
    },
    // 公司远程搜索
    async querySearch(queryString, cb) {
      const res = await this.getAllCompanyReqPub(queryString)
      res.forEach((element) => {
        element.value = element.companyName
      })
      cb(res)
    },
    // 公司列表
    async getAllCompanyReqPub(companyName) {
      const result = await getAllCompany({ companyName, page: 1, pageSize: 20 })
      const { code, data, msg } = result
      if (code !== '0000') return this.$message.error(msg)
      return data.list
    },
    dataInfoFun(data) {
      data.forEach((el) => {
        el.water = Number(el.water)
      })
      // 第一季度
      this.listData[0].value = data[0].water + data[1].water + data[2].water
      //第二季度
      this.listData[1].value = data[3].water + data[4].water + data[5].water
      //第三季度
      this.listData[2].value = data[6].water + data[7].water + data[8].water
      //第四季度
      this.listData[3].value = data[9].water + data[10].water + data[11].water

      this.listData[0].item[0].monthValue = data[0].water // 一月份
      this.listData[0].item[1].monthValue = data[1].water // 二月份
      this.listData[0].item[2].monthValue = data[2].water // 三月份
      this.listData[1].item[0].monthValue = data[3].water // 四月份
      this.listData[1].item[1].monthValue = data[4].water // 五月份
      this.listData[1].item[2].monthValue = data[5].water // 六月份
      this.listData[2].item[0].monthValue = data[6].water // 七月份
      this.listData[2].item[1].monthValue = data[7].water // 八月份
      this.listData[2].item[2].monthValue = data[8].water // 九月份
      this.listData[3].item[0].monthValue = data[9].water // 十月份
      this.listData[3].item[1].monthValue = data[10].water // 十一月份
      this.listData[3].item[2].monthValue = data[11].water // 十二月份
    }
  }
}
